package cn.b.sky.user.service.impl;

import cn.b.sky.common.exception.ImeException;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.dao.NumberRuleDao;
import cn.b.sky.user.model.NumberOrdinal;
import cn.b.sky.user.model.NumberRule;
import cn.b.sky.user.model.condition.NumberRuleCondition;
import cn.b.sky.user.service.NumberRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 
 * <strong>Title : NumberRuleServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.use.service.impl<br>
 * </strong> <strong>Description : </strong>编码规则业务实现类<br>
 * <strong>Create on : 2015-9-25 下午03:29:47<br>
 * </strong>
 * <p>
 * 
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("numberRuleService")
public class NumberRuleServiceImpl implements NumberRuleService {
  @Resource
  NumberRuleDao numberRuleDao;

  @Override
  public List<NumberRule> selectNumberRuleList(NumberRuleCondition numberRuleCondition) {
    return numberRuleDao.selectNumberRuleList(numberRuleCondition);
  }

  @Override
  public int selectNumberRuleCount(NumberRuleCondition numberRuleCondition) {
    return numberRuleDao.selectNumberRuleCount(numberRuleCondition);
  }

  @Override
  public NumberRule selectNumberRule(String siteCode, String ruleCode) {
    return numberRuleDao.selectNumberRule(siteCode, ruleCode);
  }

  @Override
  public NumberOrdinal selectNumberOrdinal(NumberOrdinal numberOrdinal) {
    return numberRuleDao.selectNumberOrdinal(numberOrdinal);
  }

  @Override
  public void insertNumberRule(NumberRule numberRule) {
    numberRuleDao.insertNumberRule(numberRule);

  }

  @Override
  public void updateNumberRule(NumberRule numberRule) {
    numberRuleDao.updateNumberRule(numberRule);
  }

  @Override
  public void deleteNumberRule(String siteCode, String ruleCode) {
    NumberOrdinal numberOrdinal = new NumberOrdinal();
    numberOrdinal.setSiteCode(siteCode);
    numberOrdinal.setRuleCode(ruleCode);
    int n = numberRuleDao.selectNumberOrdinalCount(numberOrdinal);
    if (n > 0) {
      throw ImeException.newException("numberRuleDelError.M");
    }
    numberRuleDao.deleteNumberRule(siteCode, ruleCode);
  }

  @Override
  public String selectNextNumber(String siteCode, String ruleCode) {
    String no = "";
    NumberRule number = numberRuleDao.selectNumberRule(siteCode, ruleCode);
    if (number == null) {
      throw ImeException.newException("numberNotExist.M");
    }
    String fix = "";
    if (!StringUtils.isEmpty(number.getPreFix())) {
      fix = number.getPreFix();
    }
    if (number.getEnableDateFlag() == 0) {
      // 不包含日期
      NumberOrdinal numberOrdinal = new NumberOrdinal();
      numberOrdinal.setSiteCode(siteCode);
      numberOrdinal.setRuleCode(ruleCode);
      numberOrdinal = numberRuleDao.selectNumberOrdinal(numberOrdinal);
      if (numberOrdinal == null) {
        NumberOrdinal nOrdinal = new NumberOrdinal();
        nOrdinal.setSiteCode(siteCode);
        nOrdinal.setRuleCode(ruleCode);
        nOrdinal.setCurrentOrdinal(1);
        numberRuleDao.insertNumberOrdinal(nOrdinal);
        no = fix + lpad(1, number.getOrdinalLength());
      } else {
        no = fix + lpad(numberOrdinal.getCurrentOrdinal() + 1, number.getOrdinalLength());
        numberOrdinal.setCurrentOrdinal(numberOrdinal.getCurrentOrdinal() + 1);
        numberRuleDao.updateNumberOrdinalNoDate(numberOrdinal);
      }
    } else {
      // 带日期
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
      String date = sdf.format(new Date());
      NumberOrdinal numberOrdinal = new NumberOrdinal();
      numberOrdinal.setSiteCode(siteCode);
      numberOrdinal.setRuleCode(ruleCode);
      numberOrdinal.setDateValue(date);
      numberOrdinal = numberRuleDao.selectNumberOrdinal(numberOrdinal);
      if (numberOrdinal == null) {
        NumberOrdinal nOrdinal = new NumberOrdinal();
        nOrdinal.setSiteCode(siteCode);
        nOrdinal.setRuleCode(ruleCode);
        nOrdinal.setCurrentOrdinal(1);
        nOrdinal.setDateValue(date);
        numberRuleDao.insertNumberOrdinal(nOrdinal);
        no = fix + date + lpad(1, number.getOrdinalLength());
      } else {
        no = fix + date + lpad(numberOrdinal.getCurrentOrdinal() + 1, number.getOrdinalLength());
        numberOrdinal.setCurrentOrdinal(numberOrdinal.getCurrentOrdinal() + 1);
        numberRuleDao.updateNumberOrdinalHasDate(numberOrdinal);
      }
    }
    return no;
  }

  @Override
  public int selectNumberOrdinalCount(NumberOrdinal numberOrdinal) {
    return numberRuleDao.selectNumberOrdinalCount(numberOrdinal);
  }

  private  String lpad(int num, int length) {
    StringBuilder sb = new StringBuilder("" + num);
    int indexlength = length - sb.length();
    for (int i = 0; i < indexlength; i++) {
      sb.insert(0, "0");
    }
    return sb.toString();
  }
}
